package com.dp.hystrix;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.Optional;

/**
 * TODO 类的描述
 *
 * @author 邓平
 * @createTime 2018-04-04 17:23:29
 */
public class TestJava8 {

    /*
    Java8新特性
    1.Lambda表达式
    2.方法引用
    3.默认方法
    4.新工具
    5.Stream API
    6.DateTime API
    7.Optional类
    8.Nashorn, JavaScript 引擎
     */

/*
isPresent 判断是否包含值

 */
    public static void main(String[] args) {
//        testTime();
//        testOptional();
        User u = new User();
        u.setName(null);
        System.out.println(getName(u));

//        Optional<User> deptOptional = Optional.of(null);
//        System.out.println(deptOptional.get());

//        Optional<User>  optional = Optional.empty();
//        System.out.println(optional.get());

        System.out.println(Optional.of(null).isPresent());

    }

    public static String getName(User u) {
        return Optional.ofNullable(u)
                .map(user -> user.getName())
                .orElse("Unknown");
    }

    /*
    Optional 类的引入很好的解决空指针异常。
     */
    public static void testOptional() {
        String s = "notnull";
        String s2 = Optional.ofNullable(s).orElse("dengping");
        System.out.println(s2);
        System.out.println(Optional.ofNullable("s").get());

        Optional optional = Optional.of("cc");
//        System.out.println(Optional.empty().get());

        String value = "cool string";
        Optional.ofNullable(value) // nothing
                .filter(x -> x.equals("cool string"))// this is never run since value is null
                .isPresent(); // false
    }

    public static void testTime(){
        LocalDateTime currentTime = LocalDateTime.now();
        System.out.println(currentTime);

        LocalDate localDate = currentTime.toLocalDate();
        System.out.println(localDate);

        LocalTime localTime = currentTime.toLocalTime();
        System.out.println(localTime);

        Month month = currentTime.getMonth();
        System.out.println(month + "," + month.getValue());

        System.out.println(currentTime.getYear() + "," + currentTime.getDayOfMonth() + "," + currentTime.getDayOfWeek());

        LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2011);
        System.out.println(date2);

        LocalTime localTime1 = LocalTime.parse("11:11:11");
        System.out.println(localTime1);

        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;

    }
}
